package cs.android.view;

import static cs.java.lang.Lang.no;

import cs.android.ActivityView;
import cs.android.lang.ServerRequest;
import cs.android.viewbase.ActivityViewBase;
import cs.java.event.Task;

public class ProgressView extends ActivityViewBase {

	private ServerRequest request;

	public ProgressView(ActivityView parent, int layoutId) {
		super(parent, layoutId);
	}

	@Override protected void onResume() {
		super.onResume();
		update();
	}

	private void onRequestDone() {
		request = null;
		hide();
	}

	private void update() {
		if (no(request)) hide();
		else if (request.isDone()) onRequestDone();
		else {
			show();
			new Task(this, request.getOnDone(), "on_done") {
				public void run() {
					if (isResumed()) onRequestDone();
				}
			};
		}
	}

	public void setRequest(ServerRequest request) {
		this.request = request;
		update();
	}

}
